package com.lingyistudio.common.util;

import lombok.extern.slf4j.Slf4j;
import java.security.MessageDigest;
import java.util.concurrent.ThreadLocalRandom;
/**
 * @ClassName: MD5Model
 * @Description:
 * @Author: cheng
 * @Date: 2019/5/11
 **/
@Slf4j
public class MD5Model {
    private static ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
    private static char[] md5char= {
            '!','"','#','$','%','&','(',')','*','+',
            ',','-','.','0','1','2','3','4','5','6',
            '7','8','9',':',';','<','=','>','?','@',
            'A','B','C','D','E','F','G','H','I','J',
            'K','L','M','N','O','P','Q','R','S','T',
            'U','V','W','X','Y','Z','[',']','^','`',
            'a','b','c','d','e','f','g','h','i','j',
            'k','l','m','n','o','p','q','r','s','t',
            'u','v','w','x','y','z','{','|','}','~'
    };
    public static String randomMd5(String plainText)  {
        long random = threadLocalRandom.nextLong(1000000000000000L,9999999999999999L);
        String randomString =""+md5char[(int) (random%100%(md5char.length))]+md5char[(int) (random/100%100%(md5char.length))]+
                md5char[(int) (random/10000%100%(md5char.length))]+md5char[(int) (random/1000000%100%(md5char.length))]+
                md5char[(int) (random/100000000%100%(md5char.length))]+ md5char[(int) (random/10000000000L%100%(md5char.length))]+
                md5char[(int) (random/1000000000000L%100%(md5char.length))]+ md5char[(int) (random/100000000000000L%100%(md5char.length))];
        plainText=plainText+randomString;
        String ciphertext;
        ciphertext = getMd5(plainText);
        if ( ciphertext == null ){
            ciphertext=getMd5(plainText);
        }
        return ciphertext+randomString;
    }
    public static String getMd5(String plainText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes("UTF-8"));
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0;offset <b.length;offset++){
                i = b[offset];
                if (i<0) {
                    i += 256;
                }
                if (i<16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            //32位加密
            return buf.toString();
        } catch (Exception e){
            log.error(String.valueOf(e));
            e.printStackTrace();
            return null;
        }
    }
    //plaintext:明文 ciphertext：密文
    public static boolean verifyMD5(String plainText, String cipherText)  {
        //根据传入的密钥进行验证
        String str = cipherText.substring(32);
        String plainTextMd5 = getMd5(plainText+str);
        if( plainTextMd5 == null ){
            plainTextMd5 = getMd5(plainText+str);
        }
        if( cipherText.equals(plainTextMd5+str) ){
            return true;
        }
        return false;
    }
}